﻿using System;
using System.Diagnostics;
using System.Windows.Forms;

namespace CBox_GUI
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            UpdateProcesses();
        }

        private void UpdateProcesses()
        {
            comboBox1.Items.Clear();
            comboBox1.BeginUpdate();
            foreach (Process p in Process.GetProcesses())
            {
                comboBox1.Items.Add(p.ProcessName);
            }
            comboBox1.EndUpdate();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            UpdateProcesses();
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            using (OpenFileDialog o = new OpenFileDialog())
            {
                o.Filter = "CBox Dll|*.dll";
                if (o.ShowDialog() == DialogResult.OK)
                    textBox1.Text = o.FileName;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            DoStuff();
        }
        
        private CBoxCore cbox;

        private void DoStuff()
        {
            cbox = new CBoxCore(comboBox1.Text, textBox1.Text);
            cbox.Attached += CBoxOnAttached;
            cbox.ApiCalled += CBoxOnApiCalled;

            cbox.Initialize();
        }

        private delegate void _AddStuff(string apiName, string[] param);
        private void AddStuff(string apiName, string[] param)
        {
            listView1.BeginUpdate();
            ListViewItem i = listView1.Items.Add(apiName);

            for (int j = 0; j < param.Length; j++)
            {
                if (!string.IsNullOrEmpty(param[j]))
                    i.SubItems.Add(param[j]);
            }
            listView1.EndUpdate();
        }

        private void CBoxOnApiCalled(object o, CBoxCore.ApiCalledEvent e)
        {
            try
            {
                CBoxCore.ApiData data = e.ApiData;
                _AddStuff doStuff = new _AddStuff(AddStuff);
                CBoxCore.ApiName name = data.ApiName;

                string apiName = data.ApiName.ToString();
                string[] param = data.Params;

                Invoke(doStuff, new object[] {apiName, param});
                cbox.AllowCall(true);

                //switch (name)
                //{
                //    case CBoxCore.ApiName.MessageBoxW:
                //        Invoke(doStuff, new object[] {apiName, param});
                //        cbox.AllowCall(true);//Confirm(apiName);
                //        break;

                //    case CBoxCore.ApiName.TerminateProcess:
                //        Invoke(doStuff, new object[] {apiName, param});
                //        cbox.AllowCall(true);//Confirm(apiName);
                //        break;

                //    case CBoxCore.ApiName.CreateProcess:
                //        Invoke(doStuff, new object[] {apiName, param});
                //        cbox.AllowCall(true);
                //        break;

                //    case CBoxCore.ApiName.ERROR_NO_API:
                //        // probably some unknown error occured.. I don't know :P
                //        break;
                //}
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("Exception: {0}", ex.Message));
            }
        }

        private void Confirm(string name)
        {
            cbox.AllowCall(MessageBox.Show(string.Format("Allow '{0}' call?", name), "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes);
        }

        private void CBoxOnAttached(object o, CBoxCore.ProcessAttachedEvent e)
        {
            button3.Text = string.Format("(Stop?) Monitoring: {0} (Process Id: {1})", e.ProcessName, e.Pid);
            button3.ForeColor = System.Drawing.Color.Green;
        }


    }
}
